package com.easepal802s.project;

import android.content.Context;
import android.util.Log;

import com.easepal.project.uikit.UikitManager;
import com.easepal.project.uikit.bean.Event;
import com.easepal.project.uikit.bean.Program;
import com.easepal.project.uikit.callback.EndProgramCallback;
import com.easepal.project.uikit.callback.StartProgramCallback;
import com.easepal.project.uikit.util.Constants;
import com.ogawa.base.callback.GetUpdataCallback;
import com.ogawa.base.network.BaseResponse;
import com.ogawa.base.network.Event.UpdataEvent;
import com.ogawa.base.network.RetrofitManager;
import com.ogawa.base.network.bean.BeanUpdata;
import com.ogawa.base.utils.CommonUtil;

import rx.Subscriber;

public class CommondManager {
    private static CommondManager mInst = null;
    private static Object mLock = new Object();


    private CommondManager(){

    }
    public static CommondManager getInst(){
        if(mInst==null){
            synchronized (mLock){
                mInst = new CommondManager();
            }
        }
        return mInst;

    }
    /**
     * 启动程序事件
     */
    public void startProgramEvent(int Commond) {
        Program program = new Program();
        program.setProgram("program_value_"+Commond);
        program.setType("1");
        Event event = new Event();
        event.setType(Constants.EVENT_ID_START_PROGRAM);
        event.setEventValues(program);
        UikitManager.startProgramEvent(event, new StartProgramCallback() {
            @Override
            public void onStartProgramSuccess(Event event) {
                Log.i("uikit", "onStartProgramSuccess --- 启动程序成功 --- event = " + event);
            }

            @Override
            public void onStartProgramFailure() {
                Log.i("uikit", "onStartProgramFailure --- 启动程序失败");
            }
        });
    }

    /**
     * 结束程序事件
     */
    public void endProgramEvent(int Commond) {
        Program program = new Program();
        program.setProgram("program_value_"+Commond);
        Event event = new Event();
        event.setType(Constants.EVENT_ID_END_PROGRAM);
        event.setEventValues(program);
        UikitManager.endProgramEvent(event, new EndProgramCallback() {
            @Override
            public void onEndProgramSuccess(Event event) {
                Log.i("uikit", "onEndProgramSuccess --- 结束程序成功 --- event = " + event);
            }

            @Override
            public void onEndProgramFailure() {
                Log.i("uikit", "onEndProgramFailure --- 结束程序失败");
            }
        });
    }

    /**
     * 获取升级
     */
    public void getUpdata(Context context, final GetUpdataCallback callback){
        if(callback==null){
            return;
        }
        UpdataEvent event = new UpdataEvent();
        event.setPhoneid("imei");
        event.setAppversion(String.valueOf(CommonUtil.getVersionCode(context)));//code
        event.setSystype(2);//android
        event.setMac("mac");
        event.setSysversion("28");


        RetrofitManager retrofitManager = RetrofitManager.getInstance();
        retrofitManager.setUri(com.example.reslib.Constants.URI_UPDATA);
        retrofitManager.setMethod(com.example.reslib.Constants.HTTP_POST);
        retrofitManager.setJsonBody(event);
        retrofitManager.upDataEvent(event, new Subscriber<BaseResponse<BeanUpdata>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Log.e("onError",e.toString()+"");
                callback.onGetOnlineFailure();

            }

            @Override
            public void onNext(BaseResponse<BeanUpdata> program) {
                Log.e("fhb", "GETONLINE --- 成功 ---= " + program);
                if(program.getErrCode().equals("0")&&program.getData()!=null){

                    callback.onGetOnlineSuccess(program);
                }
                else {
                    callback.onGetOnlineFailure();
                }


            }
        });


    }


}
